package com.xuetang9.t9.hotel.services.impl;

import com.xuetang9.t9.hotel.beans.ETakeoutFood;
import com.xuetang9.t9.hotel.beans.ETakeoutOrder;
import com.xuetang9.t9.hotel.mapper.ETakeoutFoodMapper;
import com.xuetang9.t9.hotel.mapper.ETakeoutOrderMapper;
import com.xuetang9.t9.hotel.mapper.condition.impl.TakeoutOrderCondition;
import com.xuetang9.t9.hotel.services.ITakeoutSerive;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Classname TakeoutService
 * @Description TODO
 * @Date 2020/3/6 16:52
 * @Created by ASUS
 */
@Service
public class TakeoutService implements ITakeoutSerive {

    @Autowired
    private ETakeoutOrderMapper takeoutOrderMapper;
    @Autowired
    private ETakeoutFoodMapper takeoutFoodMapper;

    @Override
    public boolean insertTakeout(ETakeoutOrder order) {
        int row = takeoutOrderMapper.insertOrder(order);
        Long id = order.getId();
        order.getTakeoutFoods().forEach((food) -> {
            takeoutFoodMapper.insertFood(new ETakeoutFood(id,food.getFoodId(),food.getCount()));
        });
        return id != null;
    }

    @Override
    public boolean removeTakeout(Long id) {
        takeoutFoodMapper.removeFood(id);
        int row = takeoutOrderMapper.removeOrder(id);
        return row > 0;
    }

    @Override
    public ETakeoutOrder qeuryById(Long id) {
        return takeoutOrderMapper.queryById(id);
    }

    @Override
    public boolean takeMeals(Long id, Date nowTime) {
        return takeoutOrderMapper.updateTakeout(id,nowTime) > 0;
    }

    @Override
    public List<ETakeoutOrder> queryAll() {
        return takeoutOrderMapper.queryAll();
    }

    @Override
    public List<ETakeoutOrder> queryByNone() {
        return takeoutOrderMapper.queryByNone();
    }

    @Override
    public List<ETakeoutOrder> queryByTake() {
        return takeoutOrderMapper.queryByTake();
    }

    @Override
    public List<ETakeoutOrder> queryByAll(TakeoutOrderCondition condition) {
        if (condition == null){
            condition = new TakeoutOrderCondition();
        }
        return takeoutOrderMapper.selectByAll(condition);
    }

    @Override
    public Long queryByCount(TakeoutOrderCondition condition) {
        return takeoutOrderMapper.selectByCount(condition);
    }
}
